<template>
    <div>
        <van-form ref="form">
            <van-field name="uploader" label="退款金额" required :rules=rules.refundAmount>
                <template #input>
                    <van-stepper v-model="updRefundFormData.refundAmount" class="stepBox" :decimal-length="2" min="0.01" :max="Math.min(updRefundFormData.amount, 99999999.99)" />
                </template>
            </van-field>
            <van-field v-model="updRefundFormData.remark" required rows="5" name="备注" label="备注" type="textarea" maxlength="256" placeholder="请输入备注" show-word-limit :rules=rules.remark />
            <van-field name="uploader" label="附件">
                <template #input>
                    <uploadCom ref="uploadRef" @uploadFileIds="uploadFileIds" accept=".jpg,.jpeg,.png"></uploadCom>
                </template>
            </van-field>
        </van-form>
        <full-loading v-if="loading"></full-loading>
    </div>
</template>

<script>
    import { queryFileList } from '@/api/common';
    import { updRefund, getRefundInfoByOrderId } from '@/api/srvAssurance/smarcatering/myOrder';
    export default {
        name: 'refund',
        data () {
            return {
                loading: false,
                updRefundFormData: {
                    refundId: '',
                    orderId: '',
                    orderStatus: '',
                    amount: 0,
                    refundAmount: null,
                    remark: '',
                    listFileCode: []
                },
                rules: {
                    refundAmount: [
                        { required: true, type: 'number', message: '请输入退款金额', trigger: 'onBlur' },
                        { pattern: /(^(\d{0,8})$)|(^(\d{0,8}\.\d{0,2})$)/, message: '整数不超过8位，小数不超过2位', trigger: 'onBlur' }
                    ],
                    remark: [
                        { required: true, message: '请输入备注', trigger: 'onBlur' },
                        { pattern: this.$commonReg.textAreaReg, message: this.$commonReg.textAreaMsg, trigger: 'onBlur' }
                    ]
                },
            }
        },
        methods: {
            // 退款
            getOrderData (item) {
                this.$refs.form.resetValidation();
                this.$refs.uploadRef.reset();
                this.updRefundFormData = {
                    refundId: '',
                    orderId: item.orderId,
                    orderStatus: item.orderStatus,
                    amount: item.amount,
                    refundAmount: null,
                    remark: '',
                    listFileCode: []
                }
                if (item.afterSalesStatus === '2') {
                    this.getRefundInfoByOrderIdData(item.orderId);
                }
            },
            // 查询退款信息
            getRefundInfoByOrderIdData (orderId) {
                this.loading = true;
                getRefundInfoByOrderId(orderId).then(res => {
                    this.loading = false;
                    if (res.code === 1000 && res.data) {
                        this.updRefundFormData.refundId = res.data.refundId;
                        this.updRefundFormData.refundAmount = res.data.refundAmount;
                        this.updRefundFormData.remark = res.data.remark;
                        this.updRefundFormData.listFileCode = res.data.listFileCode || [];
                        if (res.data.listFileCode && res.data.listFileCode.length) {
                            this.queryRefundFileInfoData(res.data.listFileCode);
                        }
                    }
                }).catch(() => {
                    this.loading = false;
                })
            },
            // 获取退款图片信息
            queryRefundFileInfoData (fileCodeArr) {
                queryFileList(fileCodeArr).then(res => {
                    if (res.code === 1000 && res.data && res.data.length) {
                        this.$refs.uploadRef.bindDefaultFiles(res.data);
                    }
                })
            },
            // 附件标识
            uploadFileIds (arr) {
                this.updRefundFormData.listFileCode = arr;
            },
            // 确定退款
            handleUpdRefundSure () {
                this.$refs.form.validate().then(() => {
                    let params = {
                        orderId: this.updRefundFormData.orderId,
                        orderStatus: this.updRefundFormData.orderStatus,
                        refundAmount: Number(this.updRefundFormData.refundAmount),
                        remark: this.updRefundFormData.remark,
                        listFileCode: this.updRefundFormData.listFileCode
                    }
                    if (this.updRefundFormData.refundId) {
                        params.refundId = this.updRefundFormData.refundId;
                    }
                    this.loading = true;
                    updRefund(params).then(res => {
                        this.loading = false;
                        if (res.code === 1000) {
                            this.$Notify({ type: 'success', message: '申请操作成功！' });
                            this.$emit('close');
                            this.$emit('refresh');
                        }
                    }).catch(() => {
                        this.loading = false;
                    })
                })
            },
        }
    }
</script>

<style scoped lang='less'>
    .stepBox /deep/.van-stepper__input {
        width: 120px;
    }
</style>